<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>

<head>
    <meta charset="utf-8">
    <title>Bootstrap 布局实例</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="js/user.js"></script>
    <link rel="stylesheet" href="css/user.css">
    <style>
        td, th {
            text-align: center;
        }

        .custom-smaller-input {
            font-size: 1.25rem;
        }
    </style>
</head>

<body>

<div class="container-fluid">
    <div class="row">

        <div class="col-sm-12" id="right">
            <ol class="breadcrumb">
                <li class="active">菜单
                </li>
                <li class="active">设备信息
                </li>
            </ol>
            <%
                String name = "";
                String number = "";
                String type = "";
                String vendor = "";
                String date = "";
                String time = "";
                String afterSales = "";
                String phone = "";
                if (request.getParameter("name") != null) name = request.getParameter("name");
                if (request.getParameter("number") != null) number = request.getParameter("number");
                if (request.getParameter("type") != null) type = request.getParameter("type");
                if (request.getParameter("vendor") != null) vendor = request.getParameter("vendor");
                if (request.getParameter("date") != null) date = request.getParameter("date");
                if (request.getParameter("time") != null) time = request.getParameter("time");
                if (request.getParameter("afterSales") != null) afterSales = request.getParameter("afterSales");
                if (request.getParameter("phone") != null) phone = request.getParameter("phone");
            %>
            <div class="panel panel-default">
                <div class="panel-heading">
                    搜索
                </div>
                <div class="panel-body">
                    <form role="form" class="form-inline" action="device_list.jsp" method="post">
                        <div class="form-group">
                            <label>设备名称</label>
                            <input type="text" class="form-control custom-smaller-input" id="name"
                                   name="name" value="<%=name%>" placeholder="请输入设备名称">
                        </div>
                        <div class="form-group">
                            <label>设备编号</label>
                            <input type="text" class="form-control custom-smaller-input" id="number"
                                   name="number" value="<%=number%>" placeholder="请输入物料名称">
                        </div>
                        <div class="form-group">
                            <label>设备类型</label>
                            <select class="form-control" name="type">
                                <option value="" <%=type.equals("") ? "selected" : "" %>>不限</option>
                                <option value="重型" <%=type.equals("重型") ? "selected" : "" %>>重型</option>
                                <option value="中型" <%=type.equals("中型") ? "selected" : "" %>>中型</option>
                                <option value="轻型" <%=type.equals("轻型") ? "selected" : "" %>>轻型</option>
                                <option value="便携式" <%=type.equals("便携式") ? "selected" : "" %>>便携式</option>
                                <option value="其它" <%=type.equals("其它") ? "selected" : "" %>>其它</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>供应商</label>
                            <input type="text" class="form-control custom-smaller-input" id="vendor"
                                   name="vendor" value="<%=vendor%>" placeholder="请输入供应商名称">
                        </div>
                        <div class="form-group">
                            <label>出厂日期</label>
                            <input type="date" class="form-control custom-smaller-input" id="date"
                                   name="date" value="<%=date%>">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">开始搜索</button>
                        </div>
                        <div class="form-group">
                            <a class="btn btn-default" href="device_add.jsp">添加</a>
                        </div>
                    </form>
                </div>
            </div>
            <!--
            列表展示
        -->
            <div class="table-responsive">
                <table class="table table-striped ">
                    <thead>
                    <tr>
                        <%--<th>序号</th>--%>
                        <th>设备名称</th>
                        <th>设备编号</th>
                        <th>设备类型</th>
                        <th>供应商名称</th>
                        <th>出厂日期</th>
                        <th>质保期</th>
                        <th>售后联系人</th>
                        <th>联系电话</th>
                        <th>质保说明</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        Connection conn = (Connection) session.getAttribute("Connection");
                        String sql = "SELECT * FROM device WHERE 1=1";
                        if (!name.isEmpty()) {
                            sql += " AND name LIKE '%" + name + "%'";
                        }
                        if (!number.isEmpty()) {
                            sql += " AND number LIKE '%" + number + "'";
                        }
                        if (!type.isEmpty()) {
                            sql += " AND type = '" + type + "'";
                        }
                        if (!vendor.isEmpty()) {
                            sql += " AND vendor LIKE '%" + vendor + "'";
                        }
                        if (!date.isEmpty()) {
                            sql += " AND date = '" + date + "'";
                        }
                        int curpage = 1;
                        if (request.getParameter("page") != null) {
                            String pg = request.getParameter("page");
                            curpage = Integer.parseInt(pg);
                        }
                        int al = 0; // al为-1时表示上一页，为1时表示下一页，默认为0
                        if (request.getParameter("alterpage") != null) {
                            String alpg = request.getParameter("alterpage");
                            al = Integer.parseInt(alpg);
                        }
                        if (curpage > 1 || al > 0) {
                            curpage += al;
                        }
                        int page_size = 6;
                        int start = curpage * page_size - page_size;
                        String pge = " LIMIT " + start + ", " + page_size;  // 注意前面添加了一个空格,没这个空格就会报错
                        sql = sql + pge;

                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        ResultSet rs = pstmt.executeQuery();

                        while (rs.next()) {
                    %>
                    <tr>
                        <td><%=rs.getString("name")%>
                        </td>
                        <td><%=rs.getString("number")%>
                        </td>
                        <td><%=rs.getString("type")%>
                        </td>
                        <td><%=rs.getString("vendor")%>
                        </td>
                        <td><%=rs.getString("date")%>
                        </td>
                        <td><%=rs.getString("time")%>
                        </td>
                        <td><%=rs.getString("afterSales")%>
                        </td>
                        <td><%=rs.getString("phone")%>
                        </td>
                        <td><%=rs.getString("illustrate")%>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-default" href="device_edit.jsp?id=<%=rs.getInt("id")%>">修改</a>
                                <a class="btn btn-danger"
                                   href="#" onclick="confirmDelete(<%=rs.getInt("id")%>)">删除</a>
                            </div>
                        </td>
                    </tr>
                    <%
                        }
                    %>
                    </tbody>
                </table>
            </div>
            <ul class="pagination" style="float: right;">
                <li>
                    <a href="device_list.jsp?alterpage=-1&page=<%=curpage%>&name=<%=name%>&number=<%=number%>&type=<%=type%>&vendor<%=vendor%>&date=<%=date%>">&laquo;</a>
                </li>
                <%
                    int psize = 5; //页面号码标签数量
                    int ppg = psize * ((curpage - 1) / psize + 1);
                    for (int i = ppg - psize + 1; i <= ppg; i++) {
                        if (i == curpage) {
                            out.println("<li class=\"active\"><a href=\"device_list.jsp?page="
                                    + i + "&name=" + name + "&number=" + number + "&type=" + type
                                    + "&vendor=" + vendor + "&date=" + date + "\">" + i + "</a></li>");

                        } else {
                            out.println("<li><a href=\"device_list.jsp?page=" + i + "&name="
                                    + name + "&number=" + number + "&type=" + type + "&vendor=" + vendor
                                    + "&date=" + date + "\">" + i + "</a></li>");
                        }
                    }
                %>
                <li>
                    <a href="device_list.jsp?alterpage=1&&page=<%=curpage%>&name=<%=name%>&number=<%=number%>&type=<%=type%>&vendor=<%=vendor%>&date=<%=date%>">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>
</div>

</body>
<script>
    function confirmDelete(id) {
        if (confirm("确定要删除吗？")) {
            window.location.href = "device_delete.jsp?id="+id+"&page=<%=curpage%>&name=<%=name%>&number=<%=number%>&type=<%=type%>&vendor=<%=vendor%>&date=<%=date%>";
        }
    }
</script>
</html>